package com.datasecops.scannerservices.operation;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.datasecops.scannerservices.service.ScanDataProcessServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.hbase.thirdparty.org.jvnet.hk2.annotations.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**oss数据库操作(老,作废)
 * @author: GeXiaoLong
 * @time: 2022/11/8 9:35
 */
@Service
@Slf4j
public class OssOperations {
    //每一批次定1000行
    private int batchLine = 1000;

    @Resource
    private ScanDataProcessServer dataProcessServer;

    private static String ENDPOINT = "oss-cn-beijing.aliyuncs.com";//外网访问节点
    private static String ACCESSKEYID = "LTAI5tS1FnMowceqWBGjephM";//访问秘钥
    private static String ACCESSKEYSECRET = "ZfIlHuBb1nu7bhH33PQMzUEWJMQYfY";//加密签名字符串和OSS用来验证签名字符串的密钥
    private static String BUKETNAME = "soulmy"; //存储空间名称
    private static String OBJECTNAME = "soul/HotDB数据库.doc";//文件的完整路径
    private static String PATHNAME = "P:\\soulmy";//文件下载到本地的位置
    private static String KEYPREFIX = "soul/admin/";//指定前缀,扫描此空间下所有的文件

    static OSS ossClient;

    public static void download() throws IOException {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);

        try {
//            // 调用ossClient.getObject返回一个OSSObject实例，该实例包含文件内容及文件元信息。
//            OSSObject ossObject = ossClient.getObject(BUKETNAME, OBJECTNAME);
//            // 调用ossObject.getObjectContent获取文件输入流，可读取此输入流获取其内容。
//            InputStream content = ossObject.getObjectContent();
//            if (content != null) {
//                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
//                while (true) {
//                    String line = reader.readLine();
//                    if (line == null) break;
//                    System.out.println("\n" + line);
//                }
//                // 数据读取完成后，获取的流必须关闭，否则会造成连接泄漏，导致请求无连接可用，程序无法正常工作。
//                content.close();
//            }

            // ossClient.getObject(new GetObjectRequest(BUKETNAME, OBJECTNAME), new File(PATHNAME));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public static void enumerate() {
        // 创建OSSClient实例。
        ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
        String KEYPREFIX = "soul";//指定前缀,扫描此空间下所有的文件
        String PATHNAME = "P:\\";//文件下载到本地的位置
        String PATHNAMES = "";
        String PATHNAMESS = "";
        try {

            // 列举文件。如果不设置keyPrefix，则列举存储空间下的所有文件。如果设置keyPrefix，则列举包含指定前缀的文件。
            ObjectListing objectListing = ossClient.listObjects(BUKETNAME, KEYPREFIX);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            int i = 0;//为了创建文件夹
            int b = 0;//为了跳过循环
            int c = 1;//为了赋值
            int d = 1;
            for (OSSObjectSummary s : sums) {

                //跳过文件夹的循环
                if (c==0){
                        PATHNAME =  PATHNAMESS;
                        c++;
                }
                i++;
                String OBJECTNAME = "";//文件的完整路径
                if (i != 1) {//不要第一个结果
                    System.out.println("\t" + s.getKey());
                    String[] division = (s.getKey()).split("/", -1);
                    char ch = '.';
                    for (int is = 1; is < division.length; is++) {
                        if (!s.getKey().contains(ch + "")) {
                            PATHNAMES += "\\" + division[is++];
                            new File(PATHNAMES).mkdirs();
                            KEYPREFIX += "/" + division[is++];
                            d++;
                            checkout(PATHNAMES,s.getKey(),d);
                            System.out.println("1111");
                            b+=2;
                            continue;
                        }
                        if (b != 0){
                            b--;
                            continue;
                        }
                        System.out.println(division[is]);
                        OBJECTNAME += s.getKey();
                        PATHNAME += "\\" + division[is];
                        ossClient.getObject(new GetObjectRequest(BUKETNAME, OBJECTNAME), new File(PATHNAME));
                        c--;
                    }
                    // new File(PATHNAME).mkdirs();

                } else {
                    String[] division = (s.getKey()).split("/", -1);
                    for (int is = 0; is < division.length; is += 2) {
                        System.out.println("\t" + s.getKey());
                        PATHNAME += division[is];
                        PATHNAMES = PATHNAME;
                        PATHNAMESS = PATHNAME;
                        c--;
                        new File(PATHNAME).mkdirs();
                    }

                }

            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public static void checkout(String PATHNAMES, String key, int d) {
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
        String KEYPREFIX = key;//指定前缀,扫描此空间下所有的文件
        //String OBJECTNAME = "";//文件的完整路径
        try {

            ObjectListing objectListing = ossClient.listObjects(BUKETNAME, KEYPREFIX);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            int i = 0;
            for (OSSObjectSummary s : sums) {
                String OBJECTNAME = "";
                i++;
                String PATHNAMESS =  PATHNAMES;
                //String PATHNAME = "P:\\";//文件下载到本地的位置
                if (i != 1) {//不要第一个结果
                    System.out.println("\t" + s.getKey());
                    String[] division = (s.getKey()).split("/", -1);
                    char ch = '.';
                    for (int is = 2; is < division.length; is+=d) {
                        if (!s.getKey().contains(ch + "")) {
                            PATHNAMESS+= "\\" + division[is];
                            new File(PATHNAMESS).mkdirs();

                        }
                        System.out.println(division[is]);
                        OBJECTNAME += s.getKey();
                        PATHNAMESS += "\\" + division[is];
                        ossClient.getObject(new GetObjectRequest(BUKETNAME, OBJECTNAME), new File(PATHNAMESS));
                    }
                } else {
//                    String[] division = (s.getKey()).split("/", -1);
//                    for (int is = 1; is < division.length; is += 2) {
//                        System.out.println("\t" + s.getKey());
//                        PATHNAMES += "\\" + division[is];
//                        new File(PATHNAMES).mkdirs();
//                        // new File(PATHNAME).mkdirs();
//
//                    }

                }
            }
        } catch (OSSException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
    public static void enumerates(){
        // 创建OSSClient实例。
        ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);

        try {
            // 列举文件。如果不设置keyPrefix，则列举存储空间下的所有文件。如果设置keyPrefix，则列举包含指定前缀的文件。
            ObjectListing objectListing = ossClient.listObjects(BUKETNAME, KEYPREFIX);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

        public static void main (String[]args) throws IOException {
            List<Object> objects = new ArrayList<>();
            objects.add("123");
            objects.add("2131");
            boolean remove = objects.remove("0");
            System.out.println(remove);
            Object o = objects.get(1);
            System.out.println(o);


//        String s = "P:/soul/admin/ssshhh/";
//           // String replace = s.replace("/", "\\");
//            System.out.println(s);
//            new File(s).mkdirs();


//        download();
    //        enumerate();
//        char ch = '.';
//        String s = "soul/soulmy.axy";
//        String ss = "soul/soulmy";
//        boolean contains = s.contains(ch + "");
//        System.out.println(contains);
//        boolean contains1 = ss.contains(ch + "");
//        System.out.println(contains1);
//         String[] ss= s.split("/",-1);
//        for (int i = 1 ;i<ss.length; i++) {
//            System.out.println(ss[i].toString());
//        }
            // new File("P:\\soul\\soulmy").mkdirs();
        }
    }



